﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using SynoManager.Rss;

namespace SynoManager.DownloadStation.Search
{
    public abstract class RssSearchEngineBase : SearchEngineBase
    {
        public override ISearchResult[] Search(string query)
        {
            // Form URL
            string url = GetSearchUri(query);

            // Get result
            RssFeed feed = RssFeed.Read(url);

            // Get items
            List<ISearchResult> results = new List<ISearchResult>();
            foreach(RssItem item in feed.Channels[0].Items)
            {
                // Create results
                RssSearchResult result = CreateSearchResult(item);
                results.Add(result);
            }
            return results.ToArray();
        }

        protected abstract string SearchUriFormat { get; }

        protected virtual string GetSearchUri(string query)
        {
            string url = String.Format(SearchUriFormat, HttpUtility.UrlEncode(query));
            return url;
        }

        protected abstract RssSearchResult CreateSearchResult(RssItem item);
    }
}
